<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGarbageCollectTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //垃圾分类->设置规则->可收回物类型管理:回收垃圾类型
        Schema::create("garbage_kinds",function(Blueprint $table){
            $table->increments('id');
            $table->string('name')->comment('可回收物类型名称');
            $table->string('unit')->comment('可回收物类型单位');
            $table->boolean('is_collect')->comment('是否回收');
            $table->timestamps();
    });
        //垃圾分类->设置规则->回收物奖励设置
        Schema::create('garbage_collect_rewards', function (Blueprint $table) {
            $table->increments('id');
            $table->Integer('areas_id')->unsigned()->comment('所属区域')->nullable();
            $table->foreign('areas_id')->references('id')->on('carareas')->onDelete('set null');
            $table->Integer('garbage_kind_id')->unsigned()->comment('回收垃圾类型')->nullable();
            $table->foreign('garbage_kind_id')->references('id')->on('garbage_kinds')->onDelete('set null');
            $table->float('reward_point')->unsigned()->comment('积分奖励');
            $table->float('reward_money')->unsigned()->comment('酬劳奖励');
            $table->integer('staff_id')->comment('修改人')->nullable()->unsigned();
            $table->foreign('staff_id')->references('id')->on('employees')->onDelete('set null');
            $table->timestamps();
        });

        //垃圾分类->商品管理->商品分类
        Schema::create("garbage_good_kinds",function(Blueprint $table){
            $table->increments('id');
            $table->string('name')->comment('分类名称')->unique();
            $table->timestamps();
        });

        //垃圾分类->商品管理->商品信息
        Schema::create("garbage_good_infos",function(Blueprint $table){
            $table->increments('id');
            $table->Integer('garbage_good_kind_id')->unsigned()->comment('所属分类')->nullable();
            $table->foreign('garbage_good_kind_id')->references('id')->on('garbage_good_kinds')->onDelete('set null');
            $table->string('name')->comment('商品名称');
            $table->Integer('city_id')->unsigned()->comment('所属城市')->nullable();
            $table->foreign('city_id')->references('id')->on('cities')->onDelete('set null');
            $table->float('price')->unsigned()->comment('所属城市价格')->unsigned()->nullable();
            $table->float('point')->unsigned()->comment('所属城市积分')->unsigned();
            $table->string('desc')->comment('商品介绍')->nullable();
            $table->boolean('on_sale')->comment('是否上架')->default(false);
            $table->unsignedBigInteger('file_id')->comment('商品图片id');
            $table->foreign('file_id')->on('file_upload')->references('id');
            $table->unsignedInteger('store')->comment('商品库存')->default(0);
            $table->timestamps();
        });




        //垃圾分类->垃圾周期管理->大件回收订单 订单的物件分类
        Schema::create("garbage_retrieve_kind",function (Blueprint $table){
            $table->integerIncrements('id');
            $table->char('name')->comment('分类名称')->unique();
            $table->timestamps();
        });

        //垃圾分类->垃圾周期管理->大件回收订单
        Schema::create("garbage_retrieve_orders",function (Blueprint $table){
            $table->bigIncrements('id');
            $table->char('no')->comment('订单编号')->unique();
            $table->bigInteger('user_id')->comment('订单用户信息')->unsigned();
            $table->foreign('user_id')->references('id')->on('weichat_users');
            $table->float('price')->comment('回收价格 由用户发起回收之后 后台人员修改')->nullable();
            $table->char('address')->comment('回收地址');
            $table->tinyInteger('status')->comment('状态 1待取货 2已取货 3.回收成功 4. 回收失败')->default(0);
            $table->timestamp('start_time')->comment('预约回收起始时间')->nullable();
            $table->timestamp('end_time')->comment('预约回收结束时间')->nullable();
            $table->float('weight')->comment('回收物重量');
            $table->string('desc')->comment('备注信息');
            $table->boolean('is_retrieve')->comment('是否已取货')->default(false);
            $table->string('name')->comment('用户的称呼')->nullable();
            $table->string('phone')->comment('手机号')->nullable();
            $table->unsignedInteger('kind_id')->comment('物件分类')->nullable();
            $table->foreign('kind_id')->references('id')->on('garbage_retrieve_kind');
            $table->timestamps();
        });

        //垃圾分类->垃圾周期管理->订单->大件回收订单图片
        Schema::create('garbage_retrieve_order_images',function (Blueprint $table){
            $table->bigIncrements('id');
            $table->unsignedBigInteger('file_id')->comment('图片id')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id');
            $table->bigInteger('order_id')->unsigned()->comment('所属订单编号');
            $table->foreign('order_id')->references('id')->on('garbage_retrieve_orders');
            $table->timestamps();
        });
        //垃圾分类->宣传教育->新闻消息
        Schema::create('garbage_disseminate_news',function (Blueprint $table){
            $table->bigIncrements('id');
            $table->string('title')->comment('标题');
            $table->text('content')->comment('内容');
            $table->integer('user_id')->comment('发布者ID号')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('employees');
            $table->integer('up_nums')->unsigned()->comment('点赞数量')->default(0);
            $table->integer('read_nums')->unsigned()->comment('阅读数量')->default(0);
            $table->tinyInteger('status')->comment('状态 1.草稿 2.已发布 3.已禁用')->default(2);
            $table->boolean('is_use')->comment('是否启用')->default(true);
            $table->timestamp('time')->comment('接受时间')->nullable();
            $table->timestamps();
        });
        //垃圾分类->会员管理->积分管理
        Schema::create('garbage_points',function (Blueprint $table){
            $table->bigIncrements('id');
            $table->char('no')->comment('流水编号');
            $table->float('event_point')->comment('积分 有正有负')->default(0);
            $table->float('final_point')->comment('变动后用户的积分')->default(0);
            $table->char('event')->comment('变动事件');
            $table->bigInteger('user_id')->comment('发布者ID号')->unsigned();
            $table->foreign('user_id')->references('id')->on('weichat_users');
            $table->unsignedTinyInteger('kind')->comment('积分变动类型 0未定义 1购买商品发生的变动 2回收物奖励 3监督反馈')->default(0);
            $table->timestamps();
        });


        //垃圾分类->垃圾周期管理->商品信息 商品兑换记录表
        Schema::create("garbage_good_purchase",function (Blueprint $table){
            $table->integerIncrements('id');
            $table->float('point')->comment('兑换商品所使用的积分');
            $table->unsignedInteger('good_id')->comment('兑换的商品')->nullable();
            $table->foreign('good_id')->references('id')->on('garbage_good_infos')->onDelete('set null');
            $table->timestamp('exchange_time')->comment('核销的时间')->nullable();
            $table->boolean('exchange')->comment('是否核销 默认不核销')->default(false);
            $table->string('code')->comment('兑换码')->unique();
            $table->bigInteger('user_id')->comment('兑换用户')->nullable()->unsigned();
            $table->foreign('user_id')->references('id')->on('weichat_users')->onDelete('set null');
            $table->unsignedInteger('num')->comment('兑换商品数量')->default(1);
            $table->unsignedBigInteger('change_id')->comment('对应积分变动表')->nullable();
            $table->foreign('change_id')->references('id')->on('garbage_points')->onDelete('set null');
            $table->timestamps();
        });

        //垃圾分类->监督反馈->问题反馈详细信息
        Schema::create('garbage_events',function (Blueprint $table){
            $table->bigIncrements('id');
            $table->char('title')->comment('反馈标题');
            $table->char('desc')->comment('内容提要');
            $table->integer('user_id')->comment('反馈人')->unsigned();
            $table->integer('admin_id')->comment('负责人')->unsigned()->nullable();
            $table->integer('area_id')->comment('区域')->unsigned()->nullable();
            $table->string('address')->comment('详细地址')->nullable();
            $table->tinyInteger('status')->comment('状态 1已反馈 2 已处理 3 处理中 4. 待处理')->default(3);
            $table->foreign('user_id')->on('employees')->references('id');
            $table->foreign('admin_id')->on('employees')->references('id');
            $table->foreign('area_id')->on('carareas')->references('id');
            $table->float('reward')->unsigned()->comment('反馈奖励数额')->default(0);
            $table->tinyInteger('reward_type')->unsigned()->comment('奖励的类型 1.积分 2.红包')->default(1);
            $table->boolean('is_reward')->unsigned()->comment('是否奖励 0 false 1 true')->default(0);
            $table->boolean('anonymous')->comment('匿名是否')->default(false);
            $table->boolean('is_recall')->comment('是否通知')->default(true);
            $table->timestamps();
        });
        //垃圾分类->监督反馈->问题反馈详细信息->文件
        Schema::create('garbage_event_files',function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('file_id')->comment('附加上传地址')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id');
            $table->bigInteger('event_id')->unsigned()->comment('附加所属的事件');
            $table->foreign('event_id')->on('garbage_events')->references('id');
            //$table->tinyInteger('file_type')->comment('附加类型 1. 图片 2.视频 3. 音频');
            $table->timestamps();
        });

        //垃圾分类->商机对接->商机对接详情
        Schema::create('garbage_interactions',function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->char('no')->comment('商机编号')->nullable()->comment('php自动生成一个编号');
            $table->text('desc')->comment('备注信息');
            $table->char('res')->comment('处理结果')->nullable();
            $table->tinyInteger('status')->comment('1. 待联系 2. 已联系 默认待联系')->default(1);
            $table->bigInteger('user_id')->comment('留言人微信id号')->unsigned();
            $table->foreign('user_id')->on('weichat_users')->references('id');
            $table->string('phone')->comment('预留手机号')->nullable();
            $table->string('name')->comment('预留人称呼')->nullable();
            $table->tinyInteger('gender')->default(0)->comment('性别 0 未知 1男 2女 默认0');
            $table->string('email')->comment('预留人电子邮箱');
            $table->timestamps();
        });

        //垃圾分类->商机对接->对接跟进记录
        Schema::create('garbage_interaction_follows',function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->bigInteger('event_id')->comment('跟进记录所属详情')->unsigned();
            $table->foreign('event_id')->on('garbage_interactions')->references('id');
            $table->char('title')->comment('跟进标题');
            $table->char('desc')->comment('跟进说明');
            $table->string('user_name')->comment('跟进人姓名');
            $table->timestamps();

        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('garbage_interaction_follows');
        Schema::dropIfExists('garbage_interactions');
        Schema::dropIfExists('garbage_event_files');
        Schema::dropIfExists('garbage_events');
        Schema::dropIfExists('garbage_good_purchase');
        Schema::dropIfExists('garbage_points');
        Schema::dropIfExists('garbage_disseminate_news');
        Schema::dropIfExists('garbage_retrieve_order_images');
        Schema::dropIfExists('garbage_retrieve_orders');
        Schema::dropIfExists('garbage_retrieve_kind');
        Schema::dropIfExists('garbage_good_infos');
        Schema::dropIfExists('garbage_good_kinds');
        Schema::dropIfExists('garbage_collect_rewards');
        Schema::dropIfExists('garbage_kinds');
    }
}
